/**
 * 权限角色实现类
 */
package com.cnfangmao.right.impl;

import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cnfangmao.mybatis.right.dao.RightMapper;
import com.cnfangmao.mybatis.right.dao.RoleMapper;
import com.cnfangmao.mybatis.right.dao.RoleRightMapper;
import com.cnfangmao.right.api.RightService;
import com.cnfangmao.right.entity.Right;
import com.cnfangmao.right.entity.Role;


@Component
public class RightServiceImpl implements RightService {
	private Map<String,Role> mapRoles=new HashMap<String,Role>();  //所有权限
	private Map<String,Right> mapRights=new HashMap<String,Right>();  //所有权限
	private Map<String,List<String>> mapRoleRights=new HashMap<String,List<String>>(); 
	
	@Autowired
	private RightMapper rightMapper;
	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private RoleRightMapper roleRightMapper;
	
	/****
	 * 获取所有的权限
	 */
	public Collection<Right> getAllRights() {

		return mapRights==null?null:mapRights.values();
	}

	/*****
	 * 获取所有的角色
	 */
	public Collection<Role> getAllRoles() {
		return mapRoles==null?null:mapRoles.values();
	}

	
	/******
	 * 获取一个角色具有的所有权限
	 */
	public Collection<Right> getAllRights4Role(Role role) {
		Collection<Right> result=null;		
		
		if(role!=null)
		{
			result=getAllRights4Role(role.getEname());
		}
		
		return result;
	}

	
	/*****
	 * 获取一个角色具有的所有权限
	 */
	public Collection<Right> getAllRights4Role(String strRole) {
		Collection<Right> result=null;		
		
		if(StringUtils.isNotEmpty(strRole))
		{
			result=getRightsOfRole(strRole);
		}
		return result;
	}

	/****
	 * 判断一个角色是否具有指定权限
	 */
	public boolean checkRightInRole(Role role, Right right) {
		boolean bResult=false;
		
		Collection<Right> rights=getAllRights4Role(role);
		if(rights!=null)
		{
			for(Right item:rights)
			{
				if(StringUtils.equals(item.getEname(), right.getEname()))
				{
					bResult=true;
					break;
				}
			}
		}
		
		return bResult;
	}

	/****
	 * 判断一个角色是否具有指定权限
	 */
	public boolean checkRightInRole(String strRole, String strRight) {
		boolean bResult=false;
		
		Role role=mapRoles.get(strRole);
		Right right=mapRights.get(strRight);
		
		if(role==null || right==null)
		{
			return bResult;
		}
		
		bResult=checkRightInRole(role,right);
		
		return bResult;
	}

	
	/****
	 * 得到权限对象
	 */
	public Right getRight(String strRight) {
		return mapRights.get(strRight);
	}

	/***
	 * 得到角色对象
	 */
	public Role getRole(String strRole) {
		return mapRoles.get(strRole);
	}

	/****
	 * 得到一个角色所具有的所有权限
	 * @param strRole
	 * @return
	 */
	private List<Right> getRightsOfRole(String strRole)
	{
		List<Right> lstRights=new LinkedList<Right>();
		
		if(StringUtils.isNotEmpty(strRole))
		{
			List<String> lstRightNames=mapRoleRights.get(strRole);
			if(lstRightNames!=null)
			{
				for(String strRight:lstRightNames)
				{
					Right right=mapRights.get(strRight);
					if(right!=null)
					{
						lstRights.add(right);
					}
				}
			}
		}
		
		return lstRights;
	}
	
	@PostConstruct
	public void initialize()
	{
		List<Role> lstRoles=roleMapper.getAllRoles();
		for(Role role:lstRoles)
		{
			mapRoles.put(role.getEname(), role);
		}
		
		List<Right> lstRights=rightMapper.getAllRights();
		for(Right right:lstRights)
		{
			mapRights.put(right.getEname(), right);
		}
		
		List<Map<String,Object>> lstRelation=roleRightMapper.getAllRoleRights();
		List<String> lstRoleRights=null;
		String strRoles="";
		
		for(Map<String,Object> item:lstRelation)
		{
			String strRightName=(String)item.get("RIGHTNAME");
			String strRoleName=(String)item.get("ROLENAME");
			
			if(!StringUtils.equals(strRoles, strRoleName))
			{
				if(lstRoleRights!=null)
				{
					mapRoleRights.put(strRoles, lstRoleRights);
				}
				
				strRoles=strRoleName;
				lstRoleRights=new LinkedList<String>();
			}
			
			lstRoleRights.add(strRightName);
		}
		
		if(lstRoleRights!=null)
		{
			mapRoleRights.put(strRoles, lstRoleRights);
		}
	}
}
